package com.shop.shoporder.wapController;

import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.TokenUtils;
import com.shop.shoporder.entity.GroupbuyFinace;
import com.shop.shoporder.entity.GroupbuygoodUserno;
import com.shop.shoporder.service.GroupbuyFinaceService;
import com.shop.shoporder.service.GroupbuyService;
import com.shop.shoporder.service.GroupbuygoodUsernoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的团购
 */
@CrossOrigin
@RequestMapping("/wap")
@Controller
public class GroupbuyCenterController {

    @Autowired
    GroupbuyService groupbuyService;

    @Autowired
    GroupbuygoodUsernoService groupbuygoodUsernoService;
    @Autowired
    GroupbuyFinaceService groupbuyFinaceService;


    /**
     * 团购全部
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getMyGroupbuy")
    public Response getGroupbuyAll(HttpServletRequest request) {
        int userNoId = 77300;
        String token = null;
        String initiateState = null;
        String participateState = null;
        String groupbuyState = null;
        String participateEndstate = null;
//        Map<String, Object> map = new HashMap<>();
        Map<String, Object> paramMap = new HashMap<>();

        token = request.getHeader("token");
        if (token != null && !"".equalsIgnoreCase(token)) {
            userNoId = TokenUtils.getUserNoId(token);
        }
        String myGroupbuySign = null;
        myGroupbuySign = request.getParameter("myGroupbuySign");
        if ("0".equals(myGroupbuySign)) {//拼团的正在进行
            initiateState = "1";//成功发起
            participateState = "1";//正在进行
            //结束状态
            groupbuyState = "1";
            participateEndstate = "1";

        } else if ("1".equals(myGroupbuySign)) {//拼团成功
            initiateState = "1";//成功发起
            participateState = "1";//正在进行
            groupbuyState = "0";
            participateEndstate = "0";
        } else {//拼团全部
            //全部
            initiateState = null;//成功发起
            participateState = null;//正在进行
            //结束状态
            groupbuyState = null;
            participateEndstate = null;
        }
        paramMap.put("initiateState", initiateState);
        paramMap.put("participateState", participateState);
        paramMap.put("groupbuyState", groupbuyState);
        paramMap.put("participateEndstate", participateEndstate);
        paramMap.put("userNoId", userNoId);

        List<GroupbuygoodUserno> groupbuygoodUsernoList = null;
        List<GroupbuygoodUserno> groupbuygoodUsernoList1 = new ArrayList<>();
        GroupbuyFinace groupbuyFinace = null;
        GroupbuygoodUserno groupbuygoodUserno=null;
            //(2)团购最大团的人数
            groupbuyFinace = groupbuyFinaceService.getMaxGroupbuyCount();
        if (userNoId != 0) {
            //（1）发起的团购
            groupbuygoodUsernoList = groupbuygoodUsernoService.getGroupbuygoodUserno(paramMap);

            for (int i = 0; i <groupbuygoodUsernoList.size(); i++) {
                groupbuygoodUserno=new GroupbuygoodUserno();
                groupbuygoodUserno.setBuycount(groupbuygoodUsernoList.get(i).getBuycount());
                groupbuygoodUserno.setMaxGroupbuyCount(groupbuyFinace.getGroupbuyCount());
                groupbuygoodUserno.setGroupbuyEndtime(groupbuygoodUsernoList.get(i).getGroupbuyEndtime());
                groupbuygoodUserno.setGroupbuygoodId(groupbuygoodUsernoList.get(i).getGroupbuygoodId());
                groupbuygoodUserno.setGroupbuyPrice(groupbuygoodUsernoList.get(i).getGroupbuyPrice());
                groupbuygoodUserno.setGroupbuyId(groupbuygoodUsernoList.get(i).getGroupbuyId());
                groupbuygoodUserno.setHeadPic(groupbuygoodUsernoList.get(i).getHeadPic());
                groupbuygoodUserno.setId(groupbuygoodUsernoList.get(i).getId());
                groupbuygoodUserno.setName(groupbuygoodUsernoList.get(i).getName());
                groupbuygoodUserno.setPic(groupbuygoodUsernoList.get(i).getPic());
                groupbuygoodUserno.setInitiateStarttime(groupbuygoodUsernoList.get(i).getInitiateStarttime());
                groupbuygoodUserno.setPrice(groupbuygoodUsernoList.get(i).getPrice());
                groupbuygoodUserno.setSpecId(groupbuygoodUsernoList.get(i).getSpecId());
                groupbuygoodUserno.setTitle(groupbuygoodUsernoList.get(i).getTitle());
                groupbuygoodUserno.setUserNoId(groupbuygoodUsernoList.get(i).getUserNoId());
                groupbuygoodUserno.setUserNo(groupbuygoodUsernoList.get(i).getUserNo());
                groupbuygoodUserno.setUserNick(groupbuygoodUsernoList.get(i).getUserNick());
                groupbuygoodUserno.setGroupbuyPrice(groupbuygoodUsernoList.get(i).getGroupbuyPrice());
                groupbuygoodUserno.setSpecName(groupbuygoodUsernoList.get(i).getSpecName());
                groupbuygoodUserno.setVouchers(groupbuygoodUsernoList.get(i).getVouchers());
                groupbuygoodUserno.setGroupbuyState(groupbuygoodUsernoList.get(i).getGroupbuyState());
                groupbuygoodUsernoList1.add(groupbuygoodUserno);
            }

//            map.put("groupbuygoodUsernoList", groupbuygoodUsernoList1);



//            //(2)团购最大团的人数
//            groupbuyFinace = groupbuyFinaceService.getMaxGroupbuyCount();
//            map.put("groupbuyFinace", groupbuyFinace);

            return new Response(MessageConstants.MSG_SUCCESS, groupbuygoodUsernoList1, 0, "", request);
        } else {
            //先去登录
            return new Response(MessageConstants.TOKEN_UNEXIT, null, 0, "", request);
        }

    }

//
//    /**
//     * 团购正在进行
//     *
//     * @param request
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping("/getGroupbuyInProgress")
//    public Response getGroupbuyInProgress(HttpServletRequest request) {
//
//        int userNoId = 77300;
//        String token = null;
//        Map<String, Object> map = new HashMap<>();
//        token = request.getHeader("token");
//        if (token != null && !"".equalsIgnoreCase(token)) {
//            userNoId = TokenUtils.getUserNoId(token);
//        }
//        List<GroupbuygoodUserno> groupbuygoodUsernoList = null;
//        GroupbuyFinace groupbuyFinace = null;
//        if (userNoId != 0) {
//            groupbuygoodUsernoList = groupbuygoodUsernoService.getGroupbuyInProgress(userNoId);
//            map.put("groupbuygoodUsernoList", groupbuygoodUsernoList);
//            //最大的拼团人数
//            groupbuyFinace = groupbuyFinaceService.getMaxGroupbuyCount();
//            map.put("groupbuyFinace", groupbuyFinace);
//
//            return new Response(MessageConstants.MSG_SUCCESS, map, 0, "", request);
//        } else {
//            return new Response(MessageConstants.TOKEN_UNEXIT, null, 0, "", request);
//        }
//
//    }
//
//
//    /**
//     * 团购成功
//     *
//     * @param request
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping("/getGroupbuySuccess")
//    public Response getGroupbuySuccess(HttpServletRequest request) {
//        Map<String, Object> map = new HashMap<>();
//        int userNoId = 77300;
//        String token = null;
//        token = request.getHeader("token");
//        if (token != null && !"".equalsIgnoreCase(token)) {
//            userNoId = TokenUtils.getUserNoId(token);
//        }
//        List<GroupbuygoodUserno> groupbuygoodUsernoList = null;
//        GroupbuyFinace groupbuyFinace = null;
//        if (userNoId != 0) {
//            groupbuygoodUsernoList = groupbuygoodUsernoService.getGroupbuySuccess(userNoId);
//            map.put("groupbuygoodUsernoList", groupbuygoodUsernoList);
//            //最大的拼团人数
//            groupbuyFinace = groupbuyFinaceService.getMaxGroupbuyCount();
//            map.put("groupbuyFinace", groupbuyFinace);
//
//            return new Response(MessageConstants.MSG_SUCCESS, map, 0, "", request);
//        } else {
//            return new Response(MessageConstants.TOKEN_UNEXIT, null, 0, "", request);
//        }
//    }

}
